package cn.train.service;

import java.math.BigDecimal;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.sql.Date;

import org.apache.ibatis.session.SqlSession;

import cn.train.dao.IAddOrderTableDao;

import cn.train.util.SqlSessionFactoryUtil;

public class AddOrderTableService {

	SqlSession session= SqlSessionFactoryUtil.getSqlSession();
	IAddOrderTableDao addOrderTableDao = session.getMapper(IAddOrderTableDao.class);
	
	public int addOrderTable(String name,String uname,String idcard,String action,String over,String cname,Time starttime,Time endtime,Date actiondate,String zuotype,BigDecimal price,int state,String onum){
		int dingdan= addOrderTableDao.addOrderTable(name, uname, idcard, action, over, cname, starttime, endtime, actiondate, zuotype, price, state, onum);
		session.commit();
		return dingdan;
	}
	public boolean updateOrderTableState(String ounm){
		boolean flag=addOrderTableDao.updateOrderTableState(ounm);
		if (flag) {
			session.commit();
		}
		return true;
	}
	
	public int getDateID(Date date){
		return addOrderTableDao.getDateID(date);
	}
	
	public int getTrainId(String cname){
		return addOrderTableDao.getTrainId(cname);
	}
	public int updateGPiao(int shu,int tid,int cid){
		int geng=addOrderTableDao.updateGPiao(shu, tid, cid);
		session.commit();
		return geng;
	}
	public int panDuanPiao(String piaotype,int shu,Date date,String cname){
		int flag=0;
		int tid=addOrderTableDao.getDateID(date);
		int cid=addOrderTableDao.getTrainId(cname);
		if("商务座".equals(piaotype)){
			addOrderTableDao.updateGPiao(shu, tid, cid);
			session.commit();
			flag=1;
			return flag;
		}
		if("普通座".equals(piaotype)){
			addOrderTableDao.updatePPiao(shu, tid, cid);
			session.commit();
			flag=2;
			return flag;
		}
		if("无座".equals(piaotype)){
			addOrderTableDao.updateZPiao(shu, tid, cid);
			session.commit();
			flag=3;
			return flag;
		}
		return flag;
	}
	
	public int HuoQuPiao(String cname,String piaotype,Date date){
		int piaonum=0;
		int tid=addOrderTableDao.getDateID(date);
		int cid=addOrderTableDao.getTrainId(cname);
		if("商务座".equals(piaotype)){
			piaonum=addOrderTableDao.qureyGao(tid, cid);
		}
		if("普通座".equals(piaotype)){
			piaonum=addOrderTableDao.qureyPu(tid, cid);
			
		}
		if("无座".equals(piaotype)){
			piaonum=addOrderTableDao.qureyPu(tid, cid);
			
		}
		return piaonum;
	}
	
	public int updatezhuangtaiState(int oid){
		int flag=addOrderTableDao.updatezhuangtaiState(oid);
		session.commit();
		return flag;
	}
	
}
